package fr.excilys.gwt.widgets.client;

import java.util.Arrays;
import java.util.List;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;

public class SubscribeForm extends Composite {

	private static final List<String> TITLES = Arrays.asList("Mme", "Mlle", "M");

	private static final String DATE_PATTERN = "dd/MM/yyyy";

	public SubscribeForm() {
		DecoratorPanel mainPanel = new DecoratorPanel();
		mainPanel.addStyleName("panel");
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		VerticalPanel form = new VerticalPanel();
		form.addStyleName("panel");
		// Titre
		Label panelTitle = new Label("Inscription");
		panelTitle.setStyleName("title");

		form.add(panelTitle);
		// Civilité
		ListBox titleListBox = new ListBox(false);
		for (String title : TITLES) {
			titleListBox.addItem(title);
		}
		FormInput titleInput = new FormInput("Civilité : ", titleListBox);
		form.add(titleInput);
		// Prénom
		TextBox firstNameTextBox = new TextBox();
		FormInput firstNameInput = new FormInput("Prénom : ", firstNameTextBox);
		form.add(firstNameInput);
		// Prénom
		TextBox lastNameTextBox = new TextBox();
		FormInput lastNameInput = new FormInput("Nom de famille : ", lastNameTextBox);
		form.add(lastNameInput);
		// Mail
		TextBox mailTextBox = new TextBox();
		FormInput mailInput = new FormInput("Adresse électronique : ", mailTextBox);
		form.add(mailInput);
		// Mot de passe
		PasswordTextBox passwordTextBox = new PasswordTextBox();
		FormInput passwordInput = new FormInput("Mot de passe : ", passwordTextBox);
		form.add(passwordInput);
		// Date de naissance
		DateTimeFormat dateFormat = DateTimeFormat.getFormat(DATE_PATTERN);
		DateBox birthDateDateBox = new DateBox();
		birthDateDateBox.setFormat(new DateBox.DefaultFormat(dateFormat));
		FormInput birthDateInput = new FormInput("Date de naissance : ", birthDateDateBox);
		form.add(birthDateInput);
		// Questions
		Label questions = new Label("Questions");
		questions.setStyleName("formLabel");
		form.add(questions);
		CheckBox infiniteCheckBox = new CheckBox("Avez vous déja compté jusqu'à l'infini ?");
		form.add(infiniteCheckBox);
		TextBox infiniteNumberTextBox = new TextBox();
		final FormInput infiniteNumberInput = new FormInput("Si oui, combien de fois : ", infiniteNumberTextBox);
		infiniteNumberInput.setVisible(false);
		form.add(infiniteNumberInput);
		infiniteCheckBox.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				infiniteNumberInput.setVisible(event.getValue());
			}
		});
		// Options
		DisclosurePanel optionsPanel = new DisclosurePanel("Options");
		VerticalPanel optionsForm = new VerticalPanel();
		CheckBox mailCheckBox = new CheckBox("M'envoyer des mails régulièrement.");
		mailCheckBox.setValue(true);
		optionsForm.add(mailCheckBox);
		CheckBox adCheckBox = new CheckBox("Recevoir des pubs en permanence.");
		adCheckBox.setValue(true);
		optionsForm.add(adCheckBox);
		optionsPanel.setContent(optionsForm);
		optionsPanel.setAnimationEnabled(true);
		form.add(optionsPanel);
		// S'inscrire
		Button submitButton = new Button("S'inscrire");
		form.add(submitButton);
		//CHuck 
		Image chuck = new Image(Resources.instance.chuck());
		// Finalisation
		horizontalPanel.add(form);
		horizontalPanel.add(chuck);
		mainPanel.add(horizontalPanel);
		initWidget(mainPanel);
	}
}
